<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\User;

class UserRepository
{
    /**
     * 创建用户
     * @param array $params
     * @return mixed
     */
    public function create(array $params)
    {
        return User::create($params);
    }

    /**
     * 创建用户
     * @param array $params
     * @return mixed
     */
    public function insertGetId(array $params)
    {
        return User::insertGetId($params);
    }

    /**
     * 根据id获取用户资料
     * @param $id
     * @return mixed
     */
    public function byId($id)
    {
        return User::find($id);
    }

    public function byIdInfo($id)
    {
        return User::with('infoAdd')->find($id);
    }

    /**
     * 根据mobile查询用户资料
     * @param $mobile
     * @return mixed
     */
    public function byMobile($mobile)
    {
        return User::where('mobile', $mobile)->first();
    }

    /**
     * 根据邀请码获取用户资料
     * @param $invitedCode
     * @return mixed
     */
    public function byInvitedCode($invitedCode)
    {
        return User::where('invited_code', $invitedCode)->first();
    }

    public function updateUserLevel($userId, $level)
    {
        return User::where('user_id', $userId)->update(['level' => $level]);
    }

    public function get()
    {
        return User::get();
    }

    public function updateCode($userId, $level)
    {
        return User::where('user_id', $userId)->update(['invited_code' => $level]);
    }

    public function isExistTrueNameUser($realname, $idCard)
    {
        return User::where([
            'realname' => $realname,
            'idcard_number' => $idCard,
        ])->first();
    }

    public function getUserList($request)
    {
        $page = $request->pageNumber - 1;
        $page = $page < 0 ? 0 : $page;
        $limit = $request->pageSize ? $request->pageSize : 50;
        $offset = $page * $limit;
        $query = User::where(function ($query) use ($request) {
            if (!empty($request->level)) {
                $query->where('level', $request->level);
            }
            if (!empty($request->is_truename_auth)) {
                $query->where('is_truename_auth', $request->is_truename_auth);
            }
            if (!empty($request->client_login)) {
                $query->where('client_login', $request->client_login);
            }
            if (!empty($request->is_lock)) {
                $query->where('is_lock', $request->is_lock);
            }
            if (!empty($request->keyword)) {
                switch ($request->field) {
                    case 'user_id':
                        $query->where('user_id', $request->keyword);
                        break;
                    case 'mobile':
                        $query->where('mobile', 'like', '%' . $request->keyword . '%');
                        break;
                    case 'realname':
                        $query->where('realname', 'like', '%' . $request->keyword . '%');
                        break;
                    case 'idcard_number':
                        $query->where('idcard_number', 'like', '%' . $request->keyword . '%');
                        break;
                    case 'invited_code':
                        $query->where('invited_code', 'like', '%' . $request->keyword . '%');
                        break;
                    case 'last_ip':
                        $query->where('last_ip', 'like', '%' . $request->keyword . '%');
                        break;
                    case 'api_version_number':
                        $query->where('api_version_number', 'like', '%' . $request->keyword . '%');
                        break;
                    case 'app_version_number':
                        $query->where('app_version_number', 'like', '%' . $request->keyword . '%');
                        break;
                }
            }
            if (!empty($request->reg_time_start) && !empty($request->reg_time_end)) {
                $query->whereBetween('reg_time', [strtotime($request->reg_time_start), strtotime($request->reg_time_end)]);
            }
            if (!empty($request->last_login_start_time) && !empty($request->last_login_end_time)) {
                $query->whereBetween('last_login', [strtotime($request->last_login_start_time), strtotime($request->last_login_end_time)]);
            }
        });
        $total = $query->count();
        $result = $query->latest('user_id')
            ->offset($offset)
            ->limit($limit)
            ->get();
        return ['result' => $result, 'count' => $total];
    }


}